package cn.aws360.boot.auth.starter.controller;

import cn.aws360.boot.auth.api.exception.AuthExceptionEnum;
import cn.aws360.boot.auth.api.helper.LoginHelper;
import cn.aws360.boot.auth.api.pojo.request.LoginUserQuery;
import cn.aws360.boot.auth.api.pojo.response.LoginUserModel;
import cn.aws360.boot.auth.api.service.AuthService;
import cn.aws360.boot.base.api.exception.ServiceException;
import cn.aws360.boot.base.api.pojo.properties.AppProperties;
import cn.aws360.boot.base.api.pojo.response.ResultModel;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

import static cn.aws360.boot.auth.api.constant.AuthConstant.*;

/**
 * @author junnan
 */
@Slf4j
@Controller
@RequiredArgsConstructor
@ApiSupport(order = MODULE_AUTH_CODE)
@Api(value = MODULE_AUTH_NAME, tags = MODULE_AUTH_TAGS)
public class AuthController {

    private final AuthService authService;
    private final AppProperties appProperties;

    @GetMapping("/login")
    @ApiOperation(value = "登录页面", hidden = true)
    public String login(Model model) {
        model.addAttribute("app", appProperties);
        return "login";
    }

    @GetMapping({"/", "/index"})
    @ApiOperation(value = "系统主页", hidden = true)
    public String index(Model model) {
        model.addAttribute("app", appProperties);
        model.addAttribute("user", LoginHelper.getLoginUser());
        return "index";
    }

    @ResponseBody
    @GetMapping("/captcha")
    @ApiOperation(value = "发送验证码")
    @ApiOperationSupport(order = 1)
    public ResultModel<Map<String, Object>> captcha() {
        return ResultModel.ok(authService.genCaptcha());
    }

    @ResponseBody
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    @ApiOperationSupport(order = 2)
    public ResultModel<String> login(@Validated @RequestBody LoginUserQuery loginUserQuery) {
        LoginUserModel loginUserModel = authService.login(loginUserQuery);
        return ResultModel.ok(LoginHelper.login(loginUserModel));
    }

    @ResponseBody
    @GetMapping("/user/info")
    @ApiOperation(value = "用户基本信息")
    @ApiOperationSupport(order = 3)
    public ResultModel<LoginUserModel> userInfo() {
        LoginUserModel loginUserModel = LoginHelper.getLoginUser();
        if (loginUserModel == null) {
            throw new ServiceException(AuthExceptionEnum.AUTH_DONT_LOGIN);
        }
        return ResultModel.ok(loginUserModel);
    }

    @ResponseBody
    @GetMapping("/user/menu")
    @ApiOperation(value = "用户菜单信息")
    @ApiOperationSupport(order = 4)
    public ResultModel<JSONObject> userMenu() {
        LoginUserModel loginUserModel = LoginHelper.getLoginUser();
        if (loginUserModel == null) {
            throw new ServiceException(AuthExceptionEnum.AUTH_DONT_LOGIN);
        }
        return ResultModel.ok(loginUserModel.getMenuPageJson());
    }

    @ResponseBody
    @GetMapping("/user/logout")
    @ApiOperation(value = "用户退出")
    @ApiOperationSupport(order = 5)
    public ResultModel<Boolean> userLogout() {
        LoginHelper.logout();
        return ResultModel.ok(true);
    }

}